package com.wanglibao.woodtrade.woodtrade.ui.fragment.adapter;

import android.content.Context;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.wanglibao.woodtrade.woodtrade.R;
import com.wanglibao.woodtrade.woodtrade.ui.fragment.adapter.model.InformationItem;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by lugg on 2016/5/14.
 */
public class HomeFragmentHotInformationListViewAdapter extends BaseAdapter {

    Context context;
    InformationItem informationItem;

    public HomeFragmentHotInformationListViewAdapter(Context context, InformationItem informationItem) {
        this.context = context;
        this.informationItem = informationItem;
    }

    @Override
    public int getCount() {
        return informationItem.getList().size();
    }

    @Override
    public Object getItem(int position) {
        return informationItem.getList().get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        int itemType = getItemViewType(position);
        InformationItem.ListBean listBean = informationItem.getList().get(position);
        if (itemType == 0){
            ViewHolder viewHolder = null;
            if (convertView == null){
                convertView = View.inflate(context,R.layout.item_lv_fragment_home_hot_information,null);
                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);
            }else{
                viewHolder = (ViewHolder) convertView.getTag();
            }
            Uri uri = Uri.parse(listBean.getImage_url());
            viewHolder.img.setImageURI(uri);
            viewHolder.name.setText(listBean.getTitle());
            viewHolder.data.setText(listBean.getCreate_time());
            viewHolder.summary.setText(listBean.getContent());
        }else{
            ViewHolderSimple viewHolderSimple = null;
            if (convertView == null){
                convertView = View.inflate(context,R.layout.item_lv_fragment_home_information_simple,null);
                viewHolderSimple = new ViewHolderSimple(convertView);
                convertView.setTag(viewHolderSimple);
            }else{
                viewHolderSimple = (ViewHolderSimple) convertView.getTag();
            }
            viewHolderSimple.name.setText(listBean.getTitle());
            viewHolderSimple.data.setText(listBean.getCreate_time());
        }
        return convertView;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0){
            return 0;
        }else{
            return 1;
        }
    }

    class ViewHolder{
        public ViewHolder(View view) {
            ButterKnife.bind(this,view);
        }

        @Bind(R.id.sdv_list_item_fragment_home_information_img)
        SimpleDraweeView img;
        @Bind(R.id.tv_list_item_fragment_home_information_name)
        TextView name;
        @Bind(R.id.tv_list_item_fragment_home_information_summary)
        TextView summary;
        @Bind(R.id.tv_list_item_fragment_home_information_data)
        TextView data;
    }
    class ViewHolderSimple{
        public ViewHolderSimple(View view) {
            ButterKnife.bind(this,view);
        }

        @Bind(R.id.tv_fragment_home_information_simple_name)
        TextView name;
        @Bind(R.id.tv_fragment_home_information_simple_data)
        TextView data;
    }
}
